using MediatR;
using RBAC.ErrorCode;
using System.ComponentModel.DataAnnotations;

namespace RBAC.Write.Api.Application.Command.ArrivalInspection
{
    /// <summary>
    /// 新增到货检验命令（主表+明细+附件）
    /// </summary>
    public class CreateArrivalInspectionCommand : IRequest<APIResult<long>>
    {
        /// <summary>
        /// 检验单号（可空，后端可自动生成）
        /// </summary>
        public string? DocCode { get; set; }

        /// <summary>
        /// 检验主题
        /// </summary>
        [Required]
        public string Subject { get; set; } = string.Empty;

        /// <summary>
        /// 检验日期（yyyy-MM-dd 或 ISO）
        /// </summary>
        [Required]
        public string InspectionDate { get; set; } = string.Empty;

        /// <summary>
        /// 检验人员
        /// </summary>
        public string? Inspector { get; set; }

        /// <summary>
        /// 所在部门
        /// </summary>
        public string? Department { get; set; }

        /// <summary>
        /// 采购单号
        /// </summary>
        public string? PurchaseOrderNo { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string? Remark { get; set; }

        /// <summary>
        /// 供应商ID（可选）
        /// </summary>
        public string? SupplierId { get; set; }

        /// <summary>
        /// 供应商名称（可选）
        /// </summary>
        public string? SupplierName { get; set; }

        /// <summary>
        /// 采购日期（yyyy-MM-dd 或 ISO，可选）
        /// </summary>
        public string? PurchaseDate { get; set; }

        /// <summary>
        /// 采购联系人（可选）
        /// </summary>
        public string? PurchaseContact { get; set; }

        /// <summary>
        /// 联系电话（可选）
        /// </summary>
        public string? ContactPhone { get; set; }

        /// <summary>
        /// 采购人员（可选）
        /// </summary>
        public string? Buyer { get; set; }

        /// <summary>
        /// 采购部门（可选）
        /// </summary>
        public string? PurchaseDept { get; set; }

        /// <summary>
        /// 关联项目名称（可选）
        /// </summary>
        public string? ProjectName { get; set; }

        /// <summary>
        /// 明细列表
        /// </summary>
        [MinLength(1, ErrorMessage = "明细不能为空")]
        public List<CreateArrivalInspectionItemDto> Items { get; set; } = new();

        /// <summary>
        /// 附件列表（来自独立上传接口返回的路径）
        /// </summary>
        public List<CreateArrivalInspectionAttachmentDto> Attachments { get; set; } = new();

        /// <summary>
        /// 创建人（可选）
        /// </summary>
        public string? CreateUser { get; set; }
    }

    public class CreateArrivalInspectionItemDto
    {
        public string? PurchaseOrderNo { get; set; }
        public string? MaterialCode { get; set; }
        public string? MaterialName { get; set; }
        public string? SpecModel { get; set; }
        public string? Unit { get; set; }
        public string? Brand { get; set; }
        public decimal PurchaseQty { get; set; }
        public decimal ReceivedQty { get; set; }
        public string? InspectionType { get; set; }
        public decimal InspectQty { get; set; }
        public decimal QualifiedQty { get; set; }
        public decimal UnqualifiedQty { get; set; }
        public string? InspectResult { get; set; }
        public string? UnqualifiedReason { get; set; }
        public string? Remark { get; set; }
    }

    public class CreateArrivalInspectionAttachmentDto
    {
        public string FileName { get; set; } = string.Empty;
        public string FilePath { get; set; } = string.Empty;
        public long FileSize { get; set; }
        public string? Uploader { get; set; }
        public string? Remark { get; set; }
        public string? UploadTime { get; set; }
    }
}
